import {BaseService} from '../shared/service/base.service';
import {Injectable} from '@angular/core';
import {ApiUrl} from '../config/api-url';
import {PageHelper} from '../utils/page-helper-model';

@Injectable()
export class RoleService extends BaseService {

  //查询角色列表
  findRoleList(pageHelper: PageHelper, type: Number, callback?) {
    let url = ApiUrl.RoleUrl
      + '/list?keyword=' + pageHelper.keyword.trim()
      + '&pageNum=' + pageHelper.currentPage
      + '&pageSize=' + pageHelper.pageSize
      + '&type=' + type
    ;
    this.get(url).subscribe(
      data => {
        callback && callback(data.data);
      }
    );
  }

  //删除角色
  deleteRole(roleId, callback?) {
    this.layer.modalDeleteConfirm('确定要删除该角色吗？', confirm => {
      if (confirm) {
        let url = ApiUrl.RoleUrl;
        this.delete(url, roleId).subscribe(
          data => {
            if (data.status == 0) {
              this.layer.notificationSuccess('删除角色成功！');
              callback && callback(data);
            } else {
              this.layer.notificationError(data.msg);
            }
          }
        );
      }
    });
  }

  //添加角色
  addRole(role, callback?) {
    console.log('添加角色');
    this.post(ApiUrl.RoleUrl, role).subscribe(
      data => {
        callback && callback(data);
      }
    );
  }

  //更新角色
  updateRole(role, callback?) {
    this.put(ApiUrl.RoleUrl, role).subscribe(
      data => {
        if (data.status == 0) {
          this.layer.notificationSuccess('角色更新成功！');
        } else {
          this.layer.notificationError(data.msg);
        }
        callback && callback(data);
      }
    );
  }


  findUserRoleInfo(callback?) {
    /**用户参数存储在服务器上 尝试一次性给他带出来*/
    this.get(ApiUrl.RoleUrl + '/getUserRoleInfo').subscribe(data => {
      callback && callback(data);
    });
  }
}
